﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using DTO;
using DAL;
using System.Configuration;
using System.ComponentModel;
using System.IO;

namespace BLL
{
    public class CallManagement
    {
        #region public Properties
        private string ServerPath = SharedData.DownloadPath;

        #endregion

        public CallDetailEntity ExecuteCall(CallDetailEntity calldetail, ref string message)
        {
            UpdateData up = new UpdateData();
            return up.ExecuteCall(calldetail, ref message);
        }

        public List<ResultStatusEntity> GetResultStatusGroup(int CallTypeID)
        {
            GetData gd = new GetData();
            return gd.getResultStatusGroup(CallTypeID);
        }

        public List<SampleEntity> getSachet()
        {
            GetData gd = new GetData();
            return gd.getSampleSachet();
        }

        public List<SampleEntity> getBooklet()
        {
            GetData gd = new GetData();
            return gd.getSampleBooklet();
        }

        public void InsertSampleDetail(string SampleCode, long Call_ID, long Customer_ID, int User_ID)
        {
            UpdateData up = new UpdateData();
            up.InsertSampleDetail(SampleCode, Call_ID, Customer_ID, User_ID);
        }

        public List<RecordEntity> getHistoryCall(long CustomerID)
        {
            CallMonitorService sv = new CallMonitorService();
            var tmp = sv.getHistoryCall(CustomerID);
            foreach (var item in tmp)
            {
                item.strDuration = HelperClass.GetDuration(item.Duration);
                if (item.CallStatus_ID != null)
                {
                    item.CallStatusName = SharedData.CallStatusList.Where(a => a.CallStatus_ID == item.CallStatus_ID).SingleOrDefault().CallStatusName;
                }
            }
            return tmp;
        }

        public List<SampleDetailEntity> getListSample(long CustomerID)
        {
            GetData gd = new GetData();
            return gd.getListSample(CustomerID);
        }

        public void UpdateSampleDetail(List<SampleDetailEntity> list)
        {
            UpdateData up = new UpdateData();
            up.UpdateSampleDetail(list);
        }

        public void DownloadFileRecord(string RecordName)
        {
            System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (ConfigurationUserLevel.None);
            string filePath = config.AppSettings.Settings["SaveCallFilePath"].Value;
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            DownloadFile(ServerPath + RecordName, filePath + RecordName);
        }

        public void InsertSurvey(byte NumberCup, bool ProductName, int Price,
            string BrandName, string EventCommercial, string ProductCommercial, long Call_ID)
        {
            UpdateData up = new UpdateData();
            up.InsertSurvey(NumberCup, ProductName, Price, BrandName, EventCommercial, ProductCommercial, Call_ID);
        }

        public void DownloadFile(string sourceUrl, string targetFolder)
        {
            WebClient downloader = new WebClient();
            // fake as if you are a browser making the request.
            downloader.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
            downloader.DownloadFileAsync(new Uri(sourceUrl), targetFolder);
            // wait for the current thread to complete, since the an async action will be on a new thread.
            while (downloader.IsBusy) { }
        }

        public List<HistoricalDataEntity> getHistoryInformation(long CustomerID)
        {
            GetData gd = new GetData();
            var list = gd.getHistoryInformation(CustomerID);
            foreach (HistoricalDataEntity item in list)
            {
                if (item.EDD != null)
                {
                    item.EDDS = HelperClass.GetEDDS(item.EDD);
                }
            }
            return list;
        }
    }
}
